package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UpRateService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.UpRateException;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.RateRule;
import com.qdlc.p2p.dal.dto.RateRuleDetail;
import com.qdlc.p2p.dal.model.RateRuleModel;

/**
 * 加息券规则相关action
 * @author wjx
 * @version 2.0
 * @date 2015年4月29日 上午11:07:18
 */
public class ManagerUpRateRuleAction extends BaseAction<RateRuleModel> implements ModelDriven<RateRuleModel>{

	@Resource
	private UpRateService upRateService;
	
	private Map<String, Object> data;
	/**
	 * 跳转到添加页面
	 * @return
	 */
	@Action("/modules/additional/UpRate/addPage")
	public String addPage(){
		return "addPage";
	}
	
	@Action("/modules/additional/UpRate/doAdd")
	public void doAdd(){
		data = new HashMap<String, Object>();
		model.validModelData();
	}

	/**
	 * 跳转到添加加息劵页面
	 * @return
	 */
	@Action("/modules/additional/upRate/upRateAppend")
	public String upRateAppend(){
		request.setAttribute("usableUpRateTypeList", upRateService.getUseRateRuleList());
		return "upRateAppend";
	}
	/**
	 * 跳转所有规则页
	 * @return
	 */
	@Action("/modules/additional/upRate/manage")
	public String manage(){
		return "manage";
		
	}
	
	/**
	 * 异步加载所有规则
	 * @throws IOException
	 */
	@Action("/modules/additional/upRate/rateRuleListJson")
	public void rateRuleListJson() throws IOException{
		data = new HashMap<String, Object>();
		PageDataList<RateRule> list = upRateService.getRateRulePage(model);
		int total = list.getPage().getTotal();
		data.put("total", total);
		data.put("rows", list.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	
	/**
	 * 禁用方法
	 * @throws IOException 
	 */
	@Action("/modules/additional/upRate/disable")
	public void disable() throws IOException{
		int result = upRateService.updateStatus(model.getId(), RateRuleModel.RATERULE_STATUS_DISABLE, RateRuleModel.RATERULE_STATUS_ENABLE);
		if (result ==1) {
			printWebSuccess();
		}else {
			printWebResult(MessageUtil.getMessage("MF103001003"), false);
		}
	}
	
	
	
	/**
	 * 启用方法
	 * @throws IOException 
	 */
	@Action("/modules/additional/upRate/enable")
	public void enable() throws IOException{
		int result = upRateService.updateStatus(model.getId(),RateRuleModel.RATERULE_STATUS_ENABLE  , RateRuleModel.RATERULE_STATUS_DISABLE);
		if (result ==1) {
			if (result ==1) {
				printWebSuccess();
			}else {
				printWebResult(MessageUtil.getMessage("MF103001004"), false);
			}
		}
	}
	
	/**
	 * 跳转添加页面
	 * @return
	 */
	@Action("/modules/additional/upRate/append")
	public String append(){
		
		return "append";
	}
	
	
	/**
	 * 保存添加方法
	 * @throws IOException 
	 */
	@Action("/modules/additional/upRate/doAppend")
	public void doAppend() throws IOException{
		try {
			data = new HashMap<String, Object>();
			model.validModelData();
			RateRule rule = model.prototypeForAppend();
			List<RateRuleDetail> list = getRateDetailList();
			upRateService.addUprateRule(rule, list);
			data.put("result", "true");
			data.put("msg", MessageUtil.getMessage("MS002002001"));
			printJson(getStringOfJpaObj(data));
		} catch (Exception e) {
			throw new UpRateException("添加加息券规则异常");
		}
	
	}
	
	@Action("/modules/additional/upRate/ruleDetail")
	public String ruleDetail(){
		long id = paramLong("id");
		RateRule rule = upRateService.getRateRuleById(id);
		if (rule.getPayWay() !=1) {
			List<RateRuleDetail> rateDetailList= upRateService.getDetailByRuleId(rule.getId());
			request.setAttribute("rateDetailList", rateDetailList);
		}
		request.setAttribute("rule", rule);
		return "ruleDetail";
	}
	
	/**
	 * 跳转编辑页面
	 * @return
	 */
	@Action("/modules/additional/upRate/modify")
	public String modify(){
		long id = paramLong("id");
		RateRule rule = upRateService.getRateRuleById(id);
		request.setAttribute("rule",rule);
		if (rule.getPayWay()!=1) {
			List<RateRuleDetail> rateDetailList= upRateService.getDetailByRuleId(rule.getId());
			request.setAttribute("rateDetailList", rateDetailList);
		}
		return "modify";
	}
	
	@Action("/modules/additional/upRate/domodify")
	public void doModify() throws IOException{
		data = new HashMap<String, Object>();
		model.validModelData();
		RateRule rule = model.prototype();
		List<RateRuleDetail> list = getRateDetailList();
		upRateService.updateRateRule(rule, list);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS002002002"));
		printJson(getStringOfJpaObj(data));
	}
	
	private List<RateRuleDetail> getRateDetailList(){
		int way = paramInt("payWay");
		List<RateRuleDetail> list = new ArrayList<RateRuleDetail>();
		if (way!=1) {
			String[] minmoney = request.getParameterValues("dminmoney"); //获取起始值集合
			String[] maxmoney = request.getParameterValues("dmaxmoney");//获取结束值集合
			String[] dupapr = request.getParameterValues("daprs"); //获取利率
			int size = minmoney.length;
			for (int i = 0; i < size; i++) { //循环封装详情对象,避免出现空值
				if (StringUtil.isBlank(minmoney[i]) || StringUtil.isBlank(maxmoney[i]) || StringUtil.isBlank(dupapr[i])) {//判断是否有空值
					continue;
				}
				RateRuleDetail detail = new RateRuleDetail();
				detail.setMin(NumberUtil.getDouble2(minmoney[i]));
				detail.setMax(NumberUtil.getDouble2(maxmoney[i]));
				detail.setRateApr(NumberUtil.getDouble(dupapr[i]));
				list.add(detail);
			}
		}
		
		return list;
	}

	/**
	 * 异步加载可用加息券规则
	 */
	@Action("/modules/additional/upRate/usableUpRateList")
	public void usableUpRateList() throws IOException {
		List<RateRule> list = upRateService.getUseRateRuleList();
		JSONArray jsonArray = new JSONArray();//new一个json数组
		if (!list.isEmpty() && list.size() > 0) {
			for (RateRule entity : list) {
				JSONObject obj = new JSONObject();
				obj.put("ruleId", entity.getId());
				obj.put("ruleName", entity.getRuleName());
				jsonArray.add(obj);//循环new jsonObject
			}
		}
		printJson(jsonArray.toString());
	}

}
